//
//  stackViewViewController.m
//  Gunterapple
//
//  Created by 热带雨林 on 2021/1/12.
//  Copyright © 2021 热带雨林. All rights reserved.
//

#import "stackViewViewController.h"
#import "moreStackViewController.h"

@interface stackViewViewController ()
@property (nonatomic, assign) NSInteger index;//下标
@property (nonatomic, strong) UIStackView *stackView;
@end

@implementation stackViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.index = 3;
    [self setUpUI];
}
#pragma mark - stackView的基本使用
- (void)setUpUI{
    //三张图片均等分布
    NSMutableArray *imageArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"stackview_%d",i+1]];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES; //图片适中填充 多余部分裁掉
        [imageArray addObject:imageView];
    }
    //图片的stackview
    UIStackView *stackView =  [[UIStackView alloc]initWithArrangedSubviews:imageArray];
    [self.view addSubview:stackView];
    self.stackView = stackView;
    
//    stackView.contentMode = UIViewContentModeScaleAspectFit;
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(SJHeight);
        make.left.equalTo(self.view);
        make.width.mas_equalTo(GDScreenW);
        make.height.mas_equalTo((GDScreenH/4) * 3);
    }];
    
    /**
     每个控件之间的间隙
     */
    stackView.spacing = 10.0;
    /**
     Axis是设置布局的方向，有水平和垂直两种方式，一个StackView只能选择一种布局模式
     */
    stackView.axis = UILayoutConstraintAxisVertical;
    /**
     Distribution是设置其管理视图的排列方式，我们选择等宽充满
     typedef NS_ENUM(NSInteger, UIStackViewDistribution) {
         //充满，当只有一个控件时可以使用
         UIStackViewDistributionFill = 0,
         //平分充满，每个控件占据相同尺寸排列充满
         UIStackViewDistributionFillEqually,
         //会优先按照约束的尺寸进行排列，如果没有充满，会拉伸最后一个排列的控件充满
         UIStackViewDistributionFillProportionally,
         //等间距排列
         UIStackViewDistributionEqualSpacing,
         //中心距离相等
         UIStackViewDistributionEqualCentering,
     } NS_ENUM_AVAILABLE_IOS(9_0);
     */
    stackView.distribution = UIStackViewDistributionFillEqually;
    /**
     Alignment是选择其管理视图的对齐模式，我们这里选择充满
     typedef NS_ENUM(NSInteger, UIStackViewAlignment) {
        //水平布局时为高度充满，竖直布局时为宽度充满
         UIStackViewAlignmentFill,
         //前边对其
         UIStackViewAlignmentLeading,
         //顶部对其
         UIStackViewAlignmentTop = UIStackViewAlignmentLeading,
         //第一个控件文字的基线对其 水平布局有效
         UIStackViewAlignmentFirstBaseline,
         //中心对其
         UIStackViewAlignmentCenter,
         //后边对其
         UIStackViewAlignmentTrailing,
         //底部对其
         UIStackViewAlignmentBottom = UIStackViewAlignmentTrailing,
         //基线对其，水平布局有效
         UIStackViewAlignmentLastBaseline,
     } NS_ENUM_AVAILABLE_IOS(9_0);
     */
    stackView.alignment = UIStackViewAlignmentFill;
    
    //文字的stackview
    /**
     在stack view中添加的view视图，创建的view可以不用设置frame
     */
    UILabel *title = [[UILabel alloc]init];
    title.text = @"the most beautiful start";
    title.textColor = [UIColor blackColor];
    title.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectZero];
//    view2.backgroundColor = [UIColor blueColor];
    
    //增加substackViwe
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置名称时也是需要添加模式的
    [shareBtn setTitle:@"share" forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [shareBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [shareBtn addTarget:self action:@selector(addPic) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:shareBtn];
    [shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view2);
        make.left.equalTo(view2).offset(5.0);
        make.width.mas_equalTo(38);
        make.height.mas_equalTo(20);
    }];
    
    //减少stackView
    UIButton *likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [likeBtn setTitle:@"like" forState:UIControlStateNormal];
    [likeBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    likeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [likeBtn addTarget:self action:@selector(subPic) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:likeBtn];
    [likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view2);
        make.left.equalTo(shareBtn.mas_right).offset(0);
        make.width.mas_equalTo(38);
        make.height.mas_equalTo(20);
    }];

    UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreBtn setTitle:@"more" forState:UIControlStateNormal];
    [moreBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    moreBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [view2 addSubview:moreBtn];
    [moreBtn addTarget:self action:@selector(moreStackView) forControlEvents:UIControlEventTouchUpInside];
    [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view2);
        make.left.equalTo(likeBtn.mas_right).offset(0);
        make.width.mas_equalTo(38);
        make.height.mas_equalTo(20);
    }];
    
    UIStackView *subStackView1 = [[UIStackView alloc]initWithFrame:CGRectZero];
    [subStackView1 addArrangedSubview:title];
    [subStackView1 addArrangedSubview:view2];
    subStackView1.spacing = 5.0;
    subStackView1.axis = UILayoutConstraintAxisHorizontal;
    subStackView1.alignment = UIStackViewAlignmentFill;
    subStackView1.distribution = UIStackViewDistributionFillEqually;


    UILabel *descTabel = [[UILabel alloc]init];
    descTabel.text = @"Please vote for the star that you think is the most beautiful and popular.";
    descTabel.font = [UIFont systemFontOfSize:13];
    descTabel.textColor = [UIColor blackColor];
    descTabel.numberOfLines = 0;
    descTabel.textAlignment = NSTextAlignmentLeft;

    UIStackView *stackView2 = [[UIStackView alloc]init];
    [self.view addSubview:stackView2];
    /**
     UIStackView是一个非绘图视图，这意味着    drawRect()永远不会被调用，并且它的背景颜色被忽略
     如果你非常想要背景颜色，可以考虑将堆栈视图放置在另一个视图中，并为该视图提供背景颜色。
     */
//    stackView2.backgroundColor = [UIColor redColor];
//    stackView2.layer.cornerRadius = 5.0f;
//    stackView2.clipsToBounds = YES;
    [stackView2 addArrangedSubview:subStackView1];
    [stackView2 addArrangedSubview:descTabel];
    [stackView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(stackView.mas_bottom).offset(10.0);
        make.left.equalTo(self.view).offset(5);
        make.width.mas_equalTo(GDScreenW - 10);
        make.height.mas_equalTo(GDScreenH/8 - 20.0);
    }];
    
    stackView2.spacing = 5.0;
    stackView2.axis = UILayoutConstraintAxisVertical;
    stackView2.alignment = UIStackViewAlignmentFill;
    stackView2.distribution = UIStackViewDistributionFillEqually;
    
}
- (void)addPic{
    if(self.index >= 6){
        NSLog(@"已达图片数目上限");
    }else{
        self.index ++;
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"stackview_%ld",(long)self.index]];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        [self.stackView addArrangedSubview:imageView];
    }
}
- (void)subPic{
    if(self.index == 0){
        NSLog(@"已达图片数目下限");
    }else{
        self.index --;
        UIView *view = [self.stackView subviews].lastObject;
        [self.stackView removeArrangedSubview:view];
        [view removeFromSuperview];
        [self.stackView layoutIfNeeded];
    }
}

- (void)moreStackView{
    moreStackViewController *moreVC = [[moreStackViewController alloc]init];
    [self.navigationController pushViewController:moreVC animated:YES];
}

@end
